<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of Radio
 *
 * @author chaporgin_a
 */
class VA_Form_ViewHelper_XHTML_WithChoice extends VA_Form_ViewHelper {
  public $type = 'radio';
  public function wrap() {
    $return = '';
    $name = $this->getElement()->getHTMLTagAttribute('name');
    foreach($this->getElement()->getChoices() as $value => $title){
      $line = '';
      $checked = '';
      if( (null !== $this->getElement()->getValue()) && in_array($value, $this->getElement()->getValue()))
              $checked = 'checked="checked"';
      $id = $this->getElement()->getHTMLTagAttribute('id', TRUE).'_'.rand(10, 100);
      $line .= sprintf('<input type="%s" value="%s" %s id="%s" %s %s />'
              , $this->type
              , $value
              , $name
              , $id
              , $this->getElement()->getHTMLTagAttribute('class')
              , $checked
              ) . "\n";
      $label = sprintf('<label class="radio" for="%s">%s</label>', $id, $title);
      if(VA_Form_FormElement_WithChoice::POS_LABEL_LEFT == $this->getElement()->getLabelPosition()) $line = $label.$line;
      elseif(VA_Form_FormElement_WithChoice::POS_LABEL_RIGHT == $this->getElement()->getLabelPosition()) $line = $line.$label;
      if( VA_Form_FormElement_WithChoice::LAYOUT_INLINE == $this->getElement()->getGroupLayout()) $return .= $line;
      elseif(VA_Form_FormElement_WithChoice::LAYOUT_LIST == $this->getElement()->getGroupLayout()) $return .= '<li>'.$line.'</li>';
    }
    if(VA_Form_FormElement_Radio::LAYOUT_LIST == $this->getElement()->getGroupLayout())
      $return = sprintf('<ul %s class="element radioGroup">%s</ul>'
              , $this->getElement()->getHTMLTagAttribute('style')
              , $return);
    else
      $return = sprintf('<div %s">%s</div>'
              , $this->getElement()->getHTMLTagAttribute('style')
              , $return);
    $label = $this->getElement()->getLabel();
    if(strpos($this->getElement()->getHTMLTagAttribute('class', TRUE), 'required') !== FALSE){
      $label .= ' <span class="required">*</span>';
    }
    $return = sprintf('<fieldset><legend>%s</legend>%s</fieldset>'
            , $label
            , $return
            );
    $this->getElement()->setLabel(null);

    return $return;
  }
  
}

